<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\RechPrice;
use App\Models\UsdtPrice;
use App\Models\Operate;

//商家价格相关设置
class PriceController extends Controller
{
    public function __construct()
    {
        view()->share('menu', 'price');
    }

    //usdt价格展示
    public function usdt_price_list(Request $request)
    {
        //始终找第一条
        $price = UsdtPrice::find(1);
        //买入价格
        $buyPrice = $sellerPrice = 0;
        if ($price) {
            $buyPrice = $price->buy_price;
            $sellerPrice = $price->seller_price;
        }

        return view('admin/price/usdt_price_list', compact('buyPrice', 'sellerPrice'));
    }

    //更新usdt价格
    public function update_usdt_price(Request $request)
    {
        $buyPrice = $request->get('buy_usdt_price');
        $sellerPrice = $request->get('seller_usdt_price');

        UsdtPrice::updateOrCreate(
            [
                'id' => 1,
            ],
            [
            'buy_price' => $buyPrice,
            'seller_price' => $sellerPrice,
            ]
        );

        return back()->with(['status' => 1, 'msg' => '更新成功']);
    }

    //价格区间列表
    public function rech_price_list()
    {
        $data = RechPrice::orderBy('created_at', 'desc')->get();

        return view('admin/price/rech_price_list', compact('data'));
    }

    //新增价格区间
    public function create_price_section()
    {
        return view('admin/price/create_usdt_price');
    }

    //保存新增
    public function save_price_section(Request $request)
    {
        $price = $request->get('price');

        $find = RechPrice::where('value', $price)->first();

        if ($find) {
            return back()->with(['status' => 0, 'msg' => '区间已存在']);
        }

        RechPrice::create([
            'value' => $price,
        ]);

        return redirect('/admin/rech_price_list');
    }

    //编辑价格区间
    public function edit_price_section(Request $request)
    {
        $id = $request->get('id');
        $data = RechPrice::find($id);

        return view('admin/price/edit_price_section', compact('data'));
    }

    //保存编辑
    public function update_price_section(Request $request)
    {
        $id = $request->get('id');
        $price = $request->get('price');

        $data = RechPrice::find($id);

        $data->update(['value' => $price]);

        return redirect('/admin/rech_price_list');
    }

    //删除
    public function delete_price_section(Request $request)
    {
        $id = $request->get('id');

        RechPrice::destroy($id);

        return redirect('/admin/rech_price_list');
    }

    //申述过期时间
    public function appeal_expire_time()
    {
        $data = Operate::find(1);

        return view('admin/price/appeal_expire_time', compact('data'));
    }

    //保存申述过期时间
    public function save_appeal_time(Request $request)
    {
        //单位小时
        $hour = $request->get('hour');
        if (!is_numeric($hour)) {
            return back()->with(['status' => 0, 'msg' => '必须为数字']);
        }
        //小时换算成秒
        $value = $hour * 60 * 60;

        Operate::updateOrCreate([
            'id' => 1,
        ], [
            'key' => 'expire_time',
            'value' => $value,
            'note' => '申诉过期时间',
        ]);

        return back()->with(['status' => 1, 'msg' => '修改成功']);
        //return redirect('/admin/appeal_expire_time');
    }
}
